package com.ruoyi.web.controller.system;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.oa.domain.ManageGroup;
import com.ruoyi.oa.service.IManageGroupService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 行政组信息操作处理
 * 
 * @author ruoyi
 */
@Controller
@RequestMapping("/system/group")
public class ManageGroupController extends BaseController
{
    private String prefix = "system/group";

    @Autowired
    private IManageGroupService groupService;

    @RequiresPermissions("system:group:view")
    @GetMapping()
    public String operlog()
    {
        return prefix + "/group";
    }

    @RequiresPermissions("system:group:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ManageGroup group)
    {
        startPage();
        List<ManageGroup> list = this.groupService.selectGroupList(group);
        return getDataTable(list);
    }

    @Log(title = "行政组管理", businessType = BusinessType.EXPORT)
    @RequiresPermissions("system:post:export")
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ManageGroup group)
    {
        List<ManageGroup> list = this.groupService.selectGroupList(group);
        ExcelUtil<ManageGroup> util = new ExcelUtil<ManageGroup>(ManageGroup.class);
        return util.exportExcel(list, "行政组数据");
    }

    @RequiresPermissions("system:group:remove")
    @Log(title = "行政组管理", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        try
        {
            return toAjax(this.groupService.deleteGroupByIds(ids));
        }
        catch (Exception e)
        {
            return error(e.getMessage());
        }
    }

    /**
     * 新增行政组
     */
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存岗位
     */
    @RequiresPermissions("system:group:add")
    @Log(title = "行政组管理", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated ManageGroup group)
    {
        if (!this.groupService.checkGroupNameUnique(group))
        {
            return error("新增行政组'" + group.getGroupName() + "'失败，行政组名称已存在");
        }
        else if (!this.groupService.checkGroupCodeUnique(group))
        {
            return error("新增行政组'" + group.getGroupName() + "'失败，行政组编码已存在");
        }
        group.setCreateBy(getLoginName());
        return toAjax(this.groupService.insertGroup(group));
    }

    /**
     * 修改岗位
     */
    @RequiresPermissions("system:group:edit")
    @GetMapping("/edit/{groupId}")
    public String edit(@PathVariable("groupId") Long groupId, ModelMap mmap)
    {
        mmap.put("group", this.groupService.selectGroupById(groupId));
        return prefix + "/edit";
    }

    /**
     * 修改保存岗位
     */
    @RequiresPermissions("system:group:edit")
    @Log(title = "行政组管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated ManageGroup group)
    {
        if (!this.groupService.checkGroupNameUnique(group))
        {
            return error("修改行政组'" + group.getGroupName() + "'失败，行政组名称已存在");
        }
        else if (!this.groupService.checkGroupCodeUnique(group))
        {
            return error("修改行政组'" + group.getGroupName() + "'失败，行政组编码已存在");
        }
        group.setUpdateBy(getLoginName());
        return toAjax(this.groupService.updateGroup(group));
    }

    /**
     * 校验岗位名称
     */
    @PostMapping("/checkGroupNameUnique")
    @ResponseBody
    public boolean checkGroupNameUnique(ManageGroup group)
    {
        return this.groupService.checkGroupNameUnique(group);
    }

    /**
     * 校验岗位编码
     */
    @PostMapping("/checkGroupCodeUnique")
    @ResponseBody
    public boolean checkGroupCodeUnique(ManageGroup group)
    {
        return groupService.checkGroupCodeUnique(group);
    }
}
